#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>

class Task
{
public:
    Task(int a, int b)
        : _a(a),
          _b(b)
    {}

    int Execute()
    {return _a + _b;}

    ~Task()
    {}
private:
    int _a;
    int _b;
};

class Result
{
public:
    Result(int result)
        : _result(result)
    {}

    int GetResult()
    {return _result;}
    ~Result()
    {}
private:
    int _result;
};

void* routine(void* args)
{
    Task* t = static_cast<Task*>(args);
    // int cnt = 10;
    // while(cnt--)
    // {
    //     std::cout << "线程名字: " << name << std::endl;
    //     sleep(1);
    // }
    sleep(1);
    Result *res = new Result(t->Execute());
    return res;
}


int main()
{
    pthread_t tid;
    Task* t = new Task(10, 20);
    Result* r = new Result(20);

    pthread_create(&tid, nullptr, routine, t);

    // int cnt = 5;
    // while(cnt--)
    // {
    //     std::cout << "main线程" << std::endl;
    //     sleep(1);
    // }

    Result* ret = nullptr;
    pthread_join(tid, (void**)&ret);
    std::cout << ret->GetResult() << std::endl;
    //std::cout << "新线程结束, 退出码: " << (long long)ret << std::endl;

    delete t;
    delete ret;
    return 0;
}